<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="停车场名称" prop="parkingName">
        <el-input
          v-model="queryParams.parkingName"
          placeholder="请输入停车场名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家id" prop="bussnessId">
        <el-input
          v-model="queryParams.bussnessId"
          placeholder="请输入商家id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车位数" prop="parkingSum">
        <el-input
          v-model="queryParams.parkingSum"
          placeholder="请输入车位数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用车位数" prop="available">
        <el-input
          v-model="queryParams.available"
          placeholder="请输入可用车位数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="移动电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入移动电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="固定电话" prop="telphone">
        <el-input
          v-model="queryParams.telphone"
          placeholder="请输入固定电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积大小 平方米" prop="size">
        <el-input
          v-model="queryParams.size"
          placeholder="请输入面积大小 平方米"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运营开始时间" prop="operationStartTime">
        <el-date-picker clearable
          v-model="queryParams.operationStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运营开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="运营结束时间" prop="operationEndTime">
        <el-date-picker clearable
          v-model="queryParams.operationEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运营结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:parking:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:parking:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:parking:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:parking:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="停车场名称" align="center" prop="parkingName" />
      <el-table-column label="商家id" align="center" prop="bussnessId" />
      <el-table-column label="车位数" align="center" prop="parkingSum" />
      <el-table-column label="可用车位数" align="center" prop="available" />
      <el-table-column label="入口位置" align="center" prop="positioning" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="移动电话" align="center" prop="phone" />
      <el-table-column label="固定电话" align="center" prop="telphone" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态0=关闭  1=开启 3=停止" align="center" prop="status" />
      <el-table-column label="面积大小 平方米" align="center" prop="size" />
      <el-table-column label="简介" align="center" prop="introduction" />
      <el-table-column label="运营开始时间" align="center" prop="operationStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operationStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运营结束时间" align="center" prop="operationEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operationEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="notice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:parking:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:parking:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="停车场名称" prop="parkingName">
          <el-input v-model="form.parkingName" placeholder="请输入停车场名称" />
        </el-form-item>
        <el-form-item label="商家id" prop="bussnessId">
          <el-input v-model="form.bussnessId" placeholder="请输入商家id" />
        </el-form-item>
        <el-form-item label="车位数" prop="parkingSum">
          <el-input v-model="form.parkingSum" placeholder="请输入车位数" />
        </el-form-item>
        <el-form-item label="可用车位数" prop="available">
          <el-input v-model="form.available" placeholder="请输入可用车位数" />
        </el-form-item>
        <el-form-item label="入口位置" prop="positioning">
          <el-input v-model="form.positioning" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="移动电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入移动电话" />
        </el-form-item>
        <el-form-item label="固定电话" prop="telphone">
          <el-input v-model="form.telphone" placeholder="请输入固定电话" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="面积大小 平方米" prop="size">
          <el-input v-model="form.size" placeholder="请输入面积大小 平方米" />
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="运营开始时间" prop="operationStartTime">
          <el-date-picker clearable
            v-model="form.operationStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择运营开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="运营结束时间" prop="operationEndTime">
          <el-date-picker clearable
            v-model="form.operationEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择运营结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="notice">
          <el-input v-model="form.notice" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParking, getParking, delParking, addParking, updateParking } from "@/api/system/parking";

export default {
  name: "Parking",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      parkingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkingName: null,
        bussnessId: null,
        parkingSum: null,
        available: null,
        positioning: null,
        address: null,
        phone: null,
        telphone: null,
        contacts: null,
        image: null,
        status: null,
        size: null,
        introduction: null,
        operationStartTime: null,
        operationEndTime: null,
        notice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parkingName: [
          { required: true, message: "停车场名称不能为空", trigger: "blur" }
        ],
        bussnessId: [
          { required: true, message: "商家id不能为空", trigger: "blur" }
        ],
        parkingSum: [
          { required: true, message: "车位数不能为空", trigger: "blur" }
        ],
        available: [
          { required: true, message: "可用车位数不能为空", trigger: "blur" }
        ],
        positioning: [
          { required: true, message: "入口位置不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "移动电话不能为空", trigger: "blur" }
        ],
        telphone: [
          { required: true, message: "固定电话不能为空", trigger: "blur" }
        ],
        contacts: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态0=关闭  1=开启 3=停止不能为空", trigger: "change" }
        ],
        size: [
          { required: true, message: "面积大小 平方米不能为空", trigger: "blur" }
        ],
        introduction: [
          { required: true, message: "简介不能为空", trigger: "blur" }
        ],
        operationStartTime: [
          { required: true, message: "运营开始时间不能为空", trigger: "blur" }
        ],
        operationEndTime: [
          { required: true, message: "运营结束时间不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        notice: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listParking(this.queryParams).then(response => {
        this.parkingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parkingName: null,
        bussnessId: null,
        parkingSum: null,
        available: null,
        positioning: null,
        address: null,
        phone: null,
        telphone: null,
        contacts: null,
        image: null,
        status: null,
        size: null,
        introduction: null,
        operationStartTime: null,
        operationEndTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        notice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getParking(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateParking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delParking(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/parking/export', {
        ...this.queryParams
      }, `parking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
